package com.crazymakercircle.dhptester.client;

import com.crazymakercircle.dhptester.command.*;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

@Slf4j
@Data
@Service("CommandController")
public class CommandController
{

    //聊天命令收集类
    @Autowired
    TestCaseUploadCommand testCaseUploadCommand;

    //查看测试用例的命令
    @Autowired
    TestCaseViewCommand testCaseViewCommand;

    //查看节点的命令
    @Autowired
    WorkerViewCommand workerViewCommand;


    @Autowired
    CaseRunCommand caseRunCommand;


    @Autowired
    CaseResultViewCommand caseResultViewCommand;

    @Autowired
    CaseResultClearCommand caseResultClearCommand;
    //菜单命令收集类
    @Autowired
    CommandMenu commandMenu;

    private Map<String, BaseCommand> commandMap;

    private String menuString;


    public void initCommandMap()
    {
        commandMap = new HashMap<>();
        commandMap.put(commandMenu.getKey(), commandMenu);
        commandMap.put(testCaseUploadCommand.getKey(), testCaseUploadCommand);
        commandMap.put(testCaseViewCommand.getKey(), testCaseViewCommand);
        commandMap.put(workerViewCommand.getKey(), workerViewCommand);
        commandMap.put(caseRunCommand.getKey(), caseRunCommand);
        commandMap.put(caseResultViewCommand.getKey(), caseResultViewCommand);
        commandMap.put(caseResultClearCommand.getKey(), caseResultClearCommand);

        commandMenu.setAllCommand(commandMap);
    }

    public void commandThreadRunning()
    {
        Thread.currentThread().setName("命令线程");

        while (true)
        {

            Scanner scanner = new Scanner(System.in);
            commandMenu.exec(scanner);
            String key = commandMenu.getCommandInput();
            BaseCommand command = commandMap.get(key);

            if (null == command)
            {
                System.err.println("无法识别[" + command + "]指令，请重新输入!");
                continue;
            }
            command.exec(scanner);


        }
    }

}
